package com.example.ass7.collision;

import com.example.ass7.geometryprimitives.Counter;
import com.example.ass7.level.GameLevel;
import com.example.ass7.sprite.Ball;
import com.example.ass7.sprite.Block;
import com.example.ass7.sprite.Formation;
import com.example.ass7.sprite.Paddle;

/**
 * @author Meirav Ben Izhak
 */
public class BlockRemover implements HitListener {
    private final GameLevel game;
    private final Counter remainingBlocks;
    private final Formation formation;

    /**
     * @param game
     *            game
     * @param remainingBlocks
     *            remainingBlocks
     * @param formation
     *            formation
     */
    public BlockRemover(GameLevel game,
            Counter remainingBlocks, Formation formation) {
        this.game = game;
        this.remainingBlocks = remainingBlocks;
        this.formation = formation;
    }

    /**
     * @param game game
     * @param remainingBlocks remainingBlocks
     */
    public BlockRemover(GameLevel game, Counter remainingBlocks) {
        this.game = game;
        this.remainingBlocks = remainingBlocks;
        this.formation = null;
    }

    /**
     * @param game game
     */
    public BlockRemover(GameLevel game) {
        this.game = game;
        this.remainingBlocks = null;
        this.formation = null;
    }

    // Blocks that are hit and reach 0 hit-points should be removed
    // from the game. Remember to remove this listener from the block
    // that is being removed from the game.
    @Override
    public void hitEvent(Block beingHit, Paddle beingHit2, Ball hitter) {
        if (hitter.alienOwn() && beingHit.isAlien()) {
            return;
        }

        if (beingHit.getHitPoints() == 0) {
            beingHit.removeFromGame(this.game);
            hitter.removeFromGame(this.game);
            if (remainingBlocks != null) {
                this.remainingBlocks.decrease(1);
            }
            if (formation != null) {
                this.formation.removeAlien(beingHit);
            }
            System.out.println("Block has been removed!");
        }
    }
}